package com.aqie.rabbit.controller;

import com.aqie.rabbit.access.AccessLimit;
import com.aqie.rabbit.bean.api.base.CodeMsg;
import com.aqie.rabbit.bean.api.base.ResponseModel;
import com.aqie.rabbit.bean.api.message.MessageCreateModel;
import com.aqie.rabbit.bean.card.MessageCard;
import com.aqie.rabbit.bean.db.Group;
import com.aqie.rabbit.bean.db.Message;
import com.aqie.rabbit.bean.db.User;
import com.aqie.rabbit.service.IGroupService;
import com.aqie.rabbit.service.IMessageService;
import com.aqie.rabbit.service.IPushService;
import com.aqie.rabbit.service.Impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/msg")
public class MessageController {
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private IMessageService messageService;

    @Autowired
    private IGroupService groupService;

    @Autowired
    private IPushService pushService;

    /**
     * 发送一条消息到服务器
     * @param model  MessageCreateModel ： 请求参数
     * @return 返回模型 MessageCard
     */
    @PostMapping
    @AccessLimit(seconds = 5,maxCount = 5)
    public ResponseModel<MessageCard> pushMessage(@RequestBody @Valid MessageCreateModel model) {

        User self =  userService.getUser();

        /**
         * 服务器已经有消息, 消息id 是客户端生成的则直接返回
         */
        Message message = messageService.findById(model.getId());
        if (message != null){
            return ResponseModel.success(new MessageCard(message));
        }
        if (model.getReceiverType() == Message.RECEIVER_TYPE_GROUP){
            return pushToGroup(self, model);
        }else if (model.getReceiverType() == Message.RECEIVER_TYPE_NONE){
            return pushToUser(self, model);
        }else {
            return ResponseModel.error(CodeMsg.MESSAGE_SEND_TYPE__ERROR);
        }

    }

    private ResponseModel<MessageCard> pushToUser(User sender, MessageCreateModel model) {
        User receiver = userService.findById(model.getReceiverId());
        if (receiver == null){
            return ResponseModel.error(CodeMsg.RECEIVER_USER_ID_EXIST_ERROR);
        }
        if (receiver.getId().equalsIgnoreCase(sender.getId())) {
            // 发送者接收者是同一个人就返回创建消息失败
            return ResponseModel.error(CodeMsg.CAN_NOT_SEND_TO_SELF_ERROR);
        }

        // Message存储数据库
        Message message = messageService.add(sender,receiver,model);
        return buildAndPushResponse(sender, message);
    }

    private ResponseModel<MessageCard> pushToGroup(User sender, MessageCreateModel model) {
        // 找群是有权限性质的找(必须在群内)
        Group group = groupService.findById(sender,model.getReceiverId());
        if (group == null){
            return ResponseModel.error(CodeMsg.CAN_NOT_FIND_YOUR_GROUP_ERROR);
        }
        // 保存信息到数据库
        Message message = messageService.add(sender, group, model);
        return buildAndPushResponse(sender, message);
    }

    private ResponseModel<MessageCard> buildAndPushResponse(User sender, Message message) {
        if (message == null){
            return ResponseModel.error(CodeMsg.MESSAGE_SAVE_DATABASE_ERROR);
        }
        // 消息推送
        pushService.pushNewMessage(sender, message);
        return ResponseModel.success(new MessageCard(message));
    }
}
